import type { UserInfoInterface } from '@/constrain/interface'
import { defineStore } from 'pinia'
import { reactive } from 'vue'
export const userModel = defineStore('user', () => {
    // 定义响应式数据
    const userInfo = reactive({
        nickname: '',
        avatar: ''
    })
    // 更新用户信息
    function saveUserInfo(params: UserInfoInterface) {
        userInfo.nickname = params.nickname
        userInfo.avatar = params.avatar
    }
    //清除用户信息
    function clearUserInfo() {
        userInfo.nickname = '';
        userInfo.avatar = ''
    }
    /* 验证用户是否登录 */
    function isLogin(callback: () => void) {
        if (uni.getStorageSync('token')) {
            // 用户登录
            callback()
        } else {
            // 未登录
            uni.showModal({
                title: '请登录',
                showCancel: true,
                cancelText: '取消',
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: (result) => {
                    if (result.confirm) {
                        uni.navigateTo({
                            url: '/pages/login/index'
                        })
                    } else {
                        console.log('用户不想登录');

                    }
                }
            });
        }
    }

    return {
        userInfo,
        saveUserInfo,
        clearUserInfo,
        isLogin
    }
})